const request = require("request")


const instanceRequest = (options) => {
    let promise = new Promise(function(resolve, reject) {
        request.post(options, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                resolve(body)
            }
            reject(error)
        })
    })
    return promise

}

module.exports = {

    async postFormData(url, param) {
        const options = {
            method: "POST",
            url: url,
            headers: { 'Content-Type': 'multipart/form-data' },
            formData: param,
            json: true,
        }

        return await instanceRequest(options)
    },

    async post(url, param) {
        const options = {
            method: "POST",
            url: url,
            body: param,
            json: true,
            encoding: null,
        }


        return await instanceRequest(options)
    },

    get: async(url, param) => {
        const options = {
            method: "GET",
            url: url,
            qs: param,
            json: true,
            encoding: null,
        }

        let promise = new Promise(function(resolve, reject) {
            request.get(options, function(error, response, body) {
                if (!error && response.statusCode == 200) {
                    resolve(body)
                }
                reject(error)
            })
        })
        return promise

    },
}